﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace LostHobbits.Input
{
    //public class CommandType
    //{
    //    enum Command
    //    {
    //        None,
    //        Forward,
    //        Down,
    //        Backward,
    //        Up,
    //        Action1,
    //        Action2,
    //        Action3
    //    }
    //}

    public class InputManager : GameComponent, IInputService
    {

        public enum CommandType : byte
        {
            None,
            Right,
            Down,
            Left,
            Up,
            Action1,
            Action2,
            Action3
        }

        private CommandType _lastCommand;

        public InputManager(Game game)
            : base(game)
        {
            RegisterService(game);
        }

        /// <summary>
        /// Reads a command from the keyboard. 
        /// </summary>
        /// <returns></returns>
        private CommandType ReadKeyboard()
        {
            KeyboardState kbState = Keyboard.GetState();
            CommandType command = CommandType.None;
            if (kbState.IsKeyDown(Keys.Right))
                command = CommandType.Right;
            if (kbState.IsKeyDown(Keys.Down))
                command = CommandType.Down;
            if (kbState.IsKeyDown(Keys.Left))
                command = CommandType.Left;
            if (kbState.IsKeyDown(Keys.Up))
                command = CommandType.Up;
            if (kbState.IsKeyDown(Keys.Space))
                command = CommandType.Action1;
            if (kbState.IsKeyDown(Keys.LeftAlt))
                command = CommandType.Action2;

            _lastCommand = command;
            return command;
        }

        private CommandType ReadController()
        {
            return CommandType.None;
        }


        #region IInputService Members

        byte IInputService.LastCommand
        {
            get { return (byte)_lastCommand; }
            set { _lastCommand = (CommandType)value; }
        }


        public void RegisterService(Game game)
        {
            game.Services.AddService(typeof(IInputService), this);
        }

        public void ReadInput()
        {
#if WINDOWS
            ReadKeyboard();
#elif XBOX  
            ReadController();
#endif
        }

        #endregion
    }

}
